/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.ana.facade;

import br.gov.ana.entities.Orgao;
import br.gov.ana.entities.OrgaoUsina;
import br.gov.ana.entities.Usina;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author lucas.nunes
 */
@Stateless
public class OrgaoUsinaFacade extends AbstractFacade<OrgaoUsina> {

    @PersistenceContext(unitName = "ResolucaoConjunta032010PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public OrgaoUsinaFacade() {
        super(OrgaoUsina.class);
    }

    public List<Usina> findByConsorcio(String param) {
        try {
            Query q = em.createQuery("SELECT o.ousUsiId FROM OrgaoUsina o WHERE upper(o.ousOrgId.orgTxConsorcio) like upper(:param1) or upper(o.ousOrgId.orgSg) like upper(:param2)");
            q.setParameter("param1", param);
            q.setParameter("param2", param);
            return (List<Usina>) q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
  
    public OrgaoUsina findOrgaoUsinaByUsina(Usina ousUsiId) {
        try {
            Query q = em.createQuery("SELECT o FROM OrgaoUsina o WHERE o.ousUsiId = :ousUsiId");
            q.setParameter("ousUsiId", ousUsiId);
            q.setMaxResults(1);
            return (OrgaoUsina) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public void deleteOrgaoUsinaByUsina(Usina ousUsiId) {
        try {
            Query q = em.createQuery("DELETE FROM OrgaoUsina o WHERE o.ousUsiId = :ousUsiId");
            q.setParameter("ousUsiId", ousUsiId);
            q.executeUpdate();
        } catch (Exception e) {
        }

    }

    public List findOrgaoUsinaByOrgaoUsina(Orgao ousOrgId, Usina usina) {
        try {
            Query q = em.createQuery("SELECT DISTINCT ou FROM OrgaoUsina ou "
                    + "WHERE (ou.ousOrgId = :ousOrgId) AND (ou.ousUsiId.usiNm like :usina) ");
            q.setParameter("ousOrgId", ousOrgId);
            q.setParameter("usina", usina.getUsiNm());

            return q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    public List<Usina> findUsinaByOrgao(Orgao ousOrgId) {
        try {
            Query q = em.createQuery("SELECT DISTINCT ou.ousUsiId FROM OrgaoUsina ou "
                    + "WHERE ou.ousOrgId = :ousOrgId "
                    + "AND ou.ousUsiId.usiUssId.ussId not in (:s1,:s2) "
                    + "ORDER BY ou.ousUsiId.usiId ");
            q.setParameter("ousOrgId", ousOrgId);
            
            q.setParameter("s1", new BigDecimal ("4"));
            q.setParameter("s2", new BigDecimal ("5"));

            return (List<Usina>) q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
